//
//  MyHeavenBodyViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyHeavenBodyViewController.h"
#import "UserTableViewCell.h"
#import "DKNightVersion.h"
//#import "LoginViewController.h"
#import "CollectViewController.h"

#import "DKNightVersionManager.h"
@interface MyHeavenBodyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UILabel *label;
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UIImageView *imageButton;

@end

@implementation MyHeavenBodyViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA, 0x9C9C9C);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"456"]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor clearColor];
    
        if ([indexPath row] == 1) {
            
        cell.textLabel.text = @"我的收藏";
    }else if ([indexPath row] == 2) {
        
        cell.textLabel.text = @"清除缓存";
        
    }else if ([indexPath row] == 3) {
        cell.textLabel.text = @"夜间模式";
        
        UISwitch *night = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 5, 40, 25)];
        [night addTarget:self action:@selector(swAction:) forControlEvents:(UIControlEventTouchUpInside)];
        night.onTintColor = [UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.8];
       
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        BOOL isOpen = [user boolForKey:@"isOpen"];
        night.on = isOpen;
        [cell addSubview:night];

        
    }else{
        cell.textLabel.text = @"";
    }
    
//    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return  400;
    }
    
    return 44;
}


//开关的方法
-(void)swAction:(UISwitch *)sw{

    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
        [self.dk_manager dawnComing];
    } else {
        [self.dk_manager nightFalling];
    }
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 2) {
        [self buttonAction];
    }
    if ([indexPath row] == 1) {
        CollectViewController *collectionVC = [[CollectViewController alloc] init];
        
        [self.navigationController pushViewController:collectionVC animated:YES];
    }
}
-(void)buttonAction
{
    //设置收藏提示框
    self.label = [[UILabel alloc]init];
    self.label.textAlignment = 1;
    
    [self.view addSubview:_label];
    //  ********  路径
    _label.text = [CleanCaches folderSizeAtPath];
    NSString *title = [NSString stringWithFormat:@"提示"];
    NSString *cancel = [NSString stringWithFormat:@"取消"];
    NSString *sure = [NSString stringWithFormat:@"确认"];
    
    //创建提示框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:self.label.text preferredStyle:UIAlertControllerStyleAlert];
    
    //确认按钮
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sure style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //    ********* 路径
        self.label.text = [CleanCaches cleanCachesAtPath];
    }];
    
    //取消按钮
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    //添加按钮事件
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    
    [self presentViewController:alertController animated:YES completion:^{
    }];
}

//#warning 清空文件夹方法
-(void)cleanAction
{
    //获取完整路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *cachePath = [path stringByAppendingPathComponent:@"caches"];
    //    NSLog(@"%@", cachePath);
    
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:cachePath]) {
        NSArray *fileNameArray = [manage subpathsAtPath:cachePath];
        for (NSString *fileName in fileNameArray) {
            //拼接绝对路径
            NSString *absolutePath = [cachePath stringByAppendingPathComponent:fileName];
            //通过文件管理者删除文件
            [manage removeItemAtPath:absolutePath error:nil];
        }
    }
    //及时更新label上文件的大小
    _label.text = [NSString stringWithFormat:@"%.2fM", [self folderSizeAtPath:cachePath]];
}

//#warning 计算单个文件大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:path]) {
        return [[manage attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}

//#warning 计算一个文件夹大小
-(float)folderSizeAtPath:(NSString *)path
{
    //创建文件管理者
    NSFileManager *manage = [NSFileManager defaultManager];
    if (![manage fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumrator = [[manage subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
   
    NSString *fileName = [NSString string];
    //文件夹大小
    float folderSize = 0;
    while ((fileName = [enumrator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}


////头像点击方法
//- (void)imageBtnAction:(UITapGestureRecognizer *)isTap{
//    
//    NSUserDefaults *state = [NSUserDefaults standardUserDefaults];
//    NSString *isLogin = [state objectForKey:@"isLogin"];
//    if ([isLogin floatValue] == 0) {
//        LoginViewController *loginVC = [[LoginViewController alloc]init];
//        [self.navigationController pushViewController:loginVC animated:YES];
//        
//    }else{
//        
//        [state setObject:@"0" forKey:@"isLogin"];
//        [state setObject:nil forKey:@"userName"];
//        [state synchronize];
//        
//        self.name.text = nil;
//        self.imageButton.image = [UIImage imageNamed:@"小女孩"];
//        
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已注销" message:nil preferredStyle:UIAlertControllerStyleAlert];
//        [self presentViewController:alert animated:YES completion:^{
//            
//            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
//            
//        }];
//    }
// }
- (void)timerAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


@end
